﻿using ChoWorkflowServer.Model;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace ChoWorkflowDashboard
{
    /// <summary>
    /// Interaction logic for ChoClusterNodesViewer.xaml
    /// </summary>
    public partial class ChoClusterNodesViewer : MetroWindow, INotifyPropertyChanged
    {
        private Lazy<IChoDataModel> _dataModel = new Lazy<IChoDataModel>(() => ChoDataModelFactory.New());
        private IChoDataModel DataModel
        {
            get { return _dataModel.Value; }
        }

        public string SelectedClusterOrNode
        {
            get
            {
                if (lstNodes.SelectedValue != null)
                    return lstNodes.SelectedValue.ToString();
                else if (lstClusters.SelectedItem != null)
                    return ((KeyValuePair<string, List<string>>)lstClusters.SelectedItem).Key;
                else
                    return null;
            }
        }

        private Dictionary<string, List<string>> _data = new Dictionary<string, List<string>>();
        public Dictionary<string, List<string>> Data
        {
            get { return _data; }
            set
            {
                _data = value;
                RaisePropertyChangedEvent("Data");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private DispatcherTimer dispatcherTimer;

        public ChoClusterNodesViewer()
        {
            Load();
            InitializeComponent();
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += (s, e) => Load();
            dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
            dispatcherTimer.Start();
            //ComponentDispatcher.ThreadIdle += new EventHandler(ComponentDispatcher_ThreadIdle);

            this.DataContext = this;
        }

        private void ComponentDispatcher_ThreadIdle(object sender, EventArgs e)
        {
            Load();
        }

        private void lstNodesListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var item = sender as ListViewItem;
            if (item != null && item.IsSelected)
            {
                lstNodes.UnselectAll();
                e.Handled = true;
            }
        }

        private void lstClustersListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var item = sender as ListViewItem;
            if (item != null && item.IsSelected)
            {
                lstClusters.UnselectAll();
                e.Handled = true;
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DialogResult = true;
            }
            catch {
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        private void Load()
        {

            try
            {
                Dictionary<string, List<string>> data = new Dictionary<string, List<string>>();
                foreach (KeyValuePair<string, string> keyValuePair in DataModel.GetClusterNodePairs())
                {
                    if (!data.ContainsKey(keyValuePair.Key))
                        data.Add(keyValuePair.Key, new List<string>());

                    if (!data[keyValuePair.Key].Contains(keyValuePair.Value))
                        data[keyValuePair.Key].Add(keyValuePair.Value);
                }

                Data = data;

                //this.DataContext = null;
                //this.DataContext = this;
            }
            catch
            {

            }
        }

        private void RaisePropertyChangedEvent(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
